/*
 * Copyright (c) 2021 Huawei Device Co.,  Ltd.
 * Licensed under the Apache License,  Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,  software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,  either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SWANLINK_FINANCE_ADAPTOR_PRINTER_TASK_H
#define SWANLINK_FINANCE_ADAPTOR_PRINTER_TASK_H

#include <pthread.h>
#include <vector>
#include <string>
#include <queue>
#include <memory>
#include <mutex>
#include "iprinteradaptor.h"
#include "HWISPRTPrinter.h"

namespace SwanLink {
namespace Finance {

class PrinterTask {
 public:
    PrinterTask();
    ~PrinterTask();
    /**
     * 开始打印
     */
    int StartPrint(int timeout, PrintListener *listener);
    int StopPrint();
    int AddTask(PrintTaskType ppt, int x, int y, PrintFormat  printFormat, const std::string& content);
    void RunTask();
    void DoTextTask(const PrintItem &task);
    void DoBitmapTask(const PrintItem &task);
    void DoQRTask(const PrintItem &task);
    void DoBarcodeTask(const PrintItem &task);

    void OnFailed(int code, const std::string& err);
    void OnSuccess(int proc, int total);

    static void* RunTaskThread(void *pri) {
        PrinterTask *adaptor = static_cast<PrinterTask*>(pri);
        adaptor->RunTask();
        return NULL;
    }

 private:
    std::queue<PrintItem> queTasks_;

    pthread_t  thread_id_;
    std::mutex mutex_;

    bool run_;
    int  status_;
    PrintListener *listener_;
};

int StatusConvert(int status);
}  // namespace Finance
}  // namespace SwanLink

#endif  // SWANLINK_FINANCE_ADAPTOR_PRINTER_TASK_H
